package com.open.cloud.rbac.controller.menu;

import com.alibaba.fastjson.JSONObject;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.entity.menu.CoreMenu;
import com.open.cloud.rbac.entity.menu.CoreMenuInterfaceRel;
import com.open.cloud.rbac.object.menu.CoreMenuDto;
import com.open.cloud.rbac.object.menu.CoreMenuInterfaceRelDto;
import com.open.cloud.rbac.object.menu.CoreMenuSelectDto;
import com.open.cloud.rbac.object.menu.CoreMenuVo;
import com.open.cloud.rbac.service.menu.ICoreMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-菜单模块业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/menu")
public class CoreMenuController {

    @Resource
    private ICoreMenuService menuService;

    @ApiOperation(value = "创建")
    @PostMapping("/add")
    public ResponseWrapper addMenuInfo(@RequestBody CoreMenuDto obj) {
        JSONObject data = menuService.addMenuItem(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updateMenuInfo(@RequestBody CoreMenuDto obj) {
        JSONObject data = menuService.updateMenuItem(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deleteMenuItem(@RequestParam(name = "id") String id) {
        JSONObject data = menuService.deleteMenuItem(id);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/tree")
    public ResponseWrapper<List<CoreMenuVo>> getMenuTree(@RequestBody CoreMenuSelectDto menuSelectDto) {
        List<CoreMenuVo> data = menuService.getMenuTree(menuSelectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "按MenuId查询关联接口")
    @GetMapping("/rel/interface")
    public ResponseWrapper getMenuRelInterface(@RequestParam(name = "menuId") String menuId) {
        List<CoreMenuInterfaceRel> data = menuService.getMenuRelInterface(menuId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "设置菜单接口关联关系")
    @PostMapping("/change/menu/rel")
    public ResponseWrapper changeMenuRelInterface(@RequestBody CoreMenuInterfaceRelDto menuInterfaceRelDto) {
        JSONObject data = menuService.setMenuRelInterface(menuInterfaceRelDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按MenuId查询关联接口")
    @GetMapping("/list/by/role/id")
    public ResponseWrapper getMenuListByRoleId(@RequestParam(name = "roleId") String roleId) {
        List<CoreMenu> data = menuService.getMenuListByRoleId(roleId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "按orgId查询菜单")
    @GetMapping("/list/by/orgId")
    public ResponseWrapper<List<CoreMenuVo>> getMenuListByOrgId(@RequestParam(name = "orgId") String orgId) {
        List<CoreMenuVo> data = menuService.getMenuListByOrgId(orgId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "按postId查询菜单")
    @GetMapping("/list/by/postId")
    public ResponseWrapper<List<CoreMenuVo>> getMenuListByPostId(@RequestParam(name = "postId") String postId) {
        List<CoreMenuVo> data = menuService.getMenuListByPostId(postId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "按userId查询菜单")
    @GetMapping("/list/by/userId")
    public ResponseWrapper<List<CoreMenuVo>> getMenuListByUserId(@RequestParam(name = "userId") String userId) {
        List<CoreMenuVo> data = menuService.getMenuListByUserId(userId);
        return ResponseWrapper.success(data);
    }


}

